using System;
using System.Collections.Generic;
using Mall.Api.Entity;
using Microsoft.AspNetCore.Mvc;
using System.Linq;
using Mall.Api.ParamModel;
using Newtonsoft.Json;
using Mall.Api.Repository;
using Mall.Api.Utils;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Authentication;

namespace Mall.Api.Controllers
{
    /// <summary>
    /// 系统设置
    /// </summary>

    // [Authorize]
    [ApiController]
    [Route("system")]
    public class WebsetController : ControllerBase
    {


        private IRepository<WebsiteInfo> _websiteInfo;
        private IRepository<WebsiteOtherInfo> _websiteOtherInfo;




        private readonly IConfiguration _configuration;

        public WebsetController(IConfiguration configuration, IRepository<WebsiteInfo> websiteInfo, IRepository<WebsiteOtherInfo> websiteOtherInfo)
        {
            _configuration = configuration;

            _websiteInfo = websiteInfo;
            _websiteOtherInfo = websiteOtherInfo;
        }

        /// <summary>
        /// 获取网站信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>


        [AllowAnonymous]
        [HttpGet("{id}")]
        public dynamic Get(int id)
        {
            var websit = _websiteInfo.GetById(id);
            var picture = _websiteOtherInfo.Table.Where(p => p.InfoName == "轮播图片").FirstOrDefault();
            var qqPicture = _websiteOtherInfo.Table.Where(qq => qq.InfoName == "qq二维码").FirstOrDefault();
            var wxPicture = _websiteOtherInfo.Table.Where(wx => wx.InfoName == "微信二维码").FirstOrDefault();

            if (websit == null)
            {
                return new
                {
                    Code = 1000,
                    Data = "",
                    Msg = "指定网站信息不存在"
                };

            }
            return JsonHelper.Serialize(new
            {
                Code = 1000,

                Data = new NewWebset
                {
                    Owner = websit.Owner,
                    Introduce = websit.Introduce,
                    Phone = websit.Phone,
                    Address = websit.Address,
                    Mailbox = websit.Mailbox,
                    Url = websit.Url,
                    WebsiteICP = websit.WebsiteICP,
                    PutOnRecords = websit.PutOnRecords,
                    Remarks = websit.Remarks,
                    CarouselsPicture=picture?.Content,
                    QQCodePicture=qqPicture?.Content,
                    WXCodePicture=wxPicture?.Content
                },
                Msg = "获取网站信息成功^_^"
            });

        }


        /// <summary>
        /// 修改网站信息
        /// </summary>
        /// <param name="id"></param>
        /// <param name="model"></param>
        /// <returns></returns>

        [HttpPut("{id}")]
        public dynamic Put(int id, NewWebset model)
        {
            var web = _websiteInfo.GetById(id);

            if (web != null)
            {
                web.Owner = model.Owner;
                web.Introduce = model.Introduce;
                web.Phone = model.Phone;
                web.Address = model.Address;
                web.Mailbox = model.Mailbox;
                web.Url = model.Url;
                web.WebsiteICP = model.WebsiteICP;
                web.PutOnRecords = model.PutOnRecords;
                web.Remarks = model.Remarks;
                web.UpdatedTime = DateTime.Now;


                _websiteInfo.Update(web);

                var picture = _websiteOtherInfo.Table.Where(p => p.InfoName == "轮播图片").FirstOrDefault();
                var qqPicture = _websiteOtherInfo.Table.Where(qq => qq.InfoName == "qq二维码").FirstOrDefault();
                var wxPicture = _websiteOtherInfo.Table.Where(wx => wx.InfoName == "微信二维码").FirstOrDefault();

                var result = new
                {
                    Code = 200,
                    Data = new
                    {
                        WebsiteInfo = web,
                        // OtherInfo = new List<OtherInfo>()
                    },
                    Msg = string.Format("你修改或删除的网站信息的id为：{0}，已经修改成功，请注意查收", id)
                };

                // 轮播
                if (picture != null)
                {
                    picture.Content = model.CarouselsPicture;
                    _websiteOtherInfo.Update(picture);
                    // result.Data.OtherInfo.Add(picture);
                }
                else
                {
                    var newOtherInfo = new WebsiteOtherInfo
                    {
                        InfoName = "轮播图片",
                        WebsiteInfo = _websiteInfo.Table.Single(s => s.Id == 1),
                        Content = model.CarouselsPicture
                    };
                    _websiteOtherInfo.Insert(newOtherInfo);

                    // result.Data.OtherInfo.Add(newOtherInfo);
                }
                //qq
                if (qqPicture != null)
                {
                    qqPicture.Content = model.QQCodePicture;
                    _websiteOtherInfo.Update(qqPicture);
                    // result.Data.OtherInfo.Add(qqPicture);
                }
                else
                {
                    var newOtherInfo = new WebsiteOtherInfo
                    {
                        InfoName = "qq二维码",
                        WebsiteInfo = _websiteInfo.Table.Single(s => s.Id == 1),
                        Content = model.QQCodePicture
                    };
                    _websiteOtherInfo.Insert(newOtherInfo);

                    // result.Data.OtherInfo.Add(newOtherInfo);
                }
                //微信
                if (qqPicture != null)
                {
                    wxPicture.Content = model.WXCodePicture;
                    _websiteOtherInfo.Update(wxPicture);
                    // result.Data.OtherInfo.Add(wxPicture);
                }
                else
                {
                    var newOtherInfo = new WebsiteOtherInfo
                    {
                        InfoName = "微信二维码",
                        WebsiteInfo = _websiteInfo.Table.Single(s => s.Id == 1),
                        Content = model.WXCodePicture
                    };
                    _websiteOtherInfo.Insert(newOtherInfo);

                    // result.Data.OtherInfo.Add(newOtherInfo);
                }

                return JsonHelper.Serialize(result);


            }
            else
            {
                return JsonHelper.Serialize(new
                {
                    Code = 1004,
                    Data = "",
                    Msg = "指定Id的网站不存在，请确认后重试"
                });
            }
        }




    }

}